package com.guofen.controller.shlj;

import com.guofen.pojo.ShljDeodorizationtowerWaterChange1;
import com.guofen.pojo.ShljWaterMeter;
import com.guofen.service.shlj.WaterMeterService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RequestMapping("/shlj/waterMeter")
@Controller
@Api(tags = "WaterMeterController", description = "水表记录")
public class WaterMeterController {

    @Autowired
    private WaterMeterService service;

    //保存
    @ApiOperation("添加记录")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Result add(@ApiParam(name="wMeterReadingstr",value="水表读数",required=true) @RequestParam String wMeterReadingstr,
                      @ApiParam(name="readingPerson",value="抄表人",required=true) @RequestParam String readingPerson,
                      @ApiParam(name="remarks",value="备注",required=true) @RequestParam String remarks){
        Double wMeterReading = Double.parseDouble(wMeterReadingstr);
        if (service.add(wMeterReading,readingPerson,remarks))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(service.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name="wMeterReading",value="水表读数",required=true) @RequestParam Double wMeterReading,
                         @ApiParam(name="readingPerson",value="抄表人",required=true) @RequestParam String readingPerson,
                         @ApiParam(name="remarks",value="备注",required=true) @RequestParam String remarks) {
        ShljWaterMeter record = new ShljWaterMeter();
        record.setId(id);
        record.setwMeterReading(wMeterReading);
        record.setReadingPerson(readingPerson);
        record.setRemarks(remarks);
        return ResultGenerator.genSuccessResult(service.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(service.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(service.getByTime(pageSize, currentPage, startTime, endTime));
    }

    @ApiOperation("获取 当日 最新 一条记录")
    @RequestMapping(value = "/getlast",method = RequestMethod.GET)
    @ResponseBody
    public Result get(){
        return  ResultGenerator.genSuccessResult(service.getlast());
    }
}
